package com.gt.wxpay.cashier.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * packageName com.gt.wxpay.cashier.common
 *
 * @author GuoTong
 * @version JDK 8
 * @className SpringBeanUtil
 * @date 2024/3/18
 * @description SpringBeanUtil工具类 Lazy获取Bean
 */

@Component
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static <T> T getBean(String thatBeanName, Class<T> thatClass) {
        return applicationContext.getBean(thatBeanName, thatClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanUtil.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

}